package com.sykj.why.mongo.service.impl;

import com.sykj.why.exception.WhyException;
import com.sykj.why.mongo.dao.PedigreeTDao;
import com.sykj.why.mongo.document.entity.ProjectCategoryT;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.PedigreeTService;
import com.sykj.why.util.DateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Eleven
 * @date 2019/7/8
 * @desc 传承谱系业务实现层
 */

@Service("PedigreeTService")
public class PedigreeTServiceImpl extends BasicServiceImpl<ProjectCategoryT> implements PedigreeTService {

    @Autowired
    private PedigreeTDao pedigreeTDao;

    @Override
    public void savePedigree(ProjectCategoryT projectCategoryT) {
        //验证谱系名称是否重复
        verifyName(projectCategoryT);
        //排序
        long count = pedigreeTDao.count(ProjectCategoryT.class, false);
        projectCategoryT.setSort((int) count);

        //设置时间
        projectCategoryT.setCreateTime(DateTool.getSystemTime());
        projectCategoryT.setUpdateTime(DateTool.getSystemTime());
        //保存
        pedigreeTDao.save(projectCategoryT);
    }

    /**
     * 验证谱系名称是否重复
     * @param projectCategoryT
     */
    public void verifyName(ProjectCategoryT projectCategoryT) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").ne(projectCategoryT.getId()).
                and("categoryName").is(projectCategoryT.getCategoryName()).and("removed").is(false));
        ProjectCategoryT pedigree = pedigreeTDao.findOne(query, ProjectCategoryT.class);
        if (pedigree != null) {
            throw new WhyException(ResultEnum.REPEAT);
        }
    }

    /**
     * 删除传承谱系
     * @param id
     */
    @Override
    public void removePedigree(String id) {
        //通过Id查询传承谱系
        ProjectCategoryT projectCategoryT = findById(id);
        //逻辑删除传承谱系
        pedigreeTDao.remove(projectCategoryT);
    }

    /**
     * 修改传承谱系
     * @param projectCategoryT
     */
    @Override
    public void updatePedigree(ProjectCategoryT projectCategoryT) {
        //通过id查询谱系
        ProjectCategoryT pedigree = findById(projectCategoryT.getId());
        //重新赋值
        //验证谱系名称是否重复
        verifyName(projectCategoryT);
        pedigree.setCategoryName(projectCategoryT.getCategoryName());
        pedigree.setUpdateTime(DateTool.getSystemTime());
        //保存或修改
        pedigreeTDao.saveOrupdate(pedigree);
    }

    @Override
    public List<ProjectCategoryT> findAll() {
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false));
        List<ProjectCategoryT> list = pedigreeTDao.findAll(query, ProjectCategoryT.class);
        return list;
    }

    /**
     * 通过Id查询传承谱系
     * @param id
     * @return
     */
    public ProjectCategoryT findById(String id) {
        //通过id查询未删除的菜单
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id).and("removed").is(false));
        ProjectCategoryT projectCategoryT = pedigreeTDao.findOne(query, ProjectCategoryT.class);
        if (projectCategoryT !=null){
            return projectCategoryT;
        }else {
            throw new WhyException(ResultEnum.OBJISNULL);
        }
    }

}
